package org.gdstash.ui.select;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDImagePool;

import javax.swing.*;

public class SelectAndOrPane extends AdjustablePanel {
    private JToggleButton rbOr;
    private JToggleButton rbAnd;
    private ButtonGroup group;

    public SelectAndOrPane() {
        this(0);
    }

    public SelectAndOrPane(final int direction) {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.rbOr).addComponent(this.rbAnd));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.rbOr)).addGroup(layout.createParallelGroup().addComponent(this.rbAnd));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.rbOr, this.rbAnd);
        layout.linkSize(1, this.rbOr, this.rbAnd);
    }

    @Override
    public void adjustUI() {
        if (this.rbOr == null) {
            (this.rbOr = new JToggleButton()).setIcon(GDImagePool.iconBoolOrUnselect);
            this.rbOr.setSelectedIcon(GDImagePool.iconBoolOr);
        }
        if (this.rbAnd == null) {
            (this.rbAnd = new JToggleButton()).setIcon(GDImagePool.iconBoolAndUnselect);
            this.rbAnd.setSelectedIcon(GDImagePool.iconBoolAnd);
        }
        if (this.group == null) {
            this.group = new ButtonGroup();
            this.rbOr.setSelected(true);
            this.group.add(this.rbOr);
            this.group.add(this.rbAnd);
        }
    }

    public void clear() {
    }

    public void addCriteria(final SelectionCriteria criteria) {
        criteria.combiMode = SelectionCriteria.CombinationMode.OR;
        if (this.rbAnd.isSelected()) {
            criteria.combiMode = SelectionCriteria.CombinationMode.AND;
        }
    }
}
